// pages/shouxia/shouxia.js

import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scene:'',
    allmsg:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.scene)
    this.setData({
      scene: options.scene
    })

    this.getallmsg()
  },

  getallmsg:function(){
    var that = this
    ajax('/checkShare', {
      data: {
        scene: that.data.scene
      },
      header: {
        "Accept": "application/json" // 默认值
      },
      method: "post",
    }, res => {
      console.log(res)

      var filename = res.data.fileInfo.fileName


      let fileNameqian = filename.split(".")//取到文件名开始到最后一个点的长度
      let fileFormat = fileNameqian[1]
      res.data.fileInfo.fileFormat = fileFormat
      
      this.setData({
        allmsg:res.data
      })
    })
  },
  



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  


  shouxia:function(e){
    if (!wx.getStorageSync("sessionKey")){
      if (e.detail.userInfo) {
        console.log(e)
        wx.setStorageSync("encryptedData", e.detail.encryptedData)
        wx.setStorageSync("iv", e.detail.iv)
        var that = this
        var code = wx.getStorageSync("code");
       
        ajax('/wxLogin', {
          data: {
            code: code,
          },
          header: {
            'Accept': 'application/json', // 默认值
          },
          method: 'post'
        }, res => {
          console.log(res)
          console.log(res)
          if (res.status == 200) {

            wx.setStorageSync('isAudit', '11111')
            
            wx.setStorageSync("token", res.data.token)
            wx.setStorageSync("sessionKey", res.data.sessionKey)

            wx.setStorageSync("isFirst", res.data.isFirst)
            var that = this
            ajax('/loadShare', {
              data:{
                share: that.data.allmsg.content
              },
              header: {
                "Accept": "application/json" // 默认值
              },
              method: "get",
            }, res => {
              console.log(res)
      
              // 自己收自己的
              if (res.code == 1) {
                wx.showToast({
                  title: res.data,
                  icon: 'none',
                  duration: 2000
                })
                wx.switchTab ({
                  url: '/pages/index/index',
                })
              } else {
                wx.showToast({
                  title: res.data,
                  icon: 'none',
                  duration: 2000
                })
              }
             
            })
          }
        })
        
      } else {
        wx.showToast({
          title: '请先授权！',
          icon: 'none',
          duration: 2000
        })
      }

    }else{
      var that = this
      ajax('/loadShare', {
        data:{
          share: that.data.allmsg.content
        },
        header: {
          "Accept": "application/json" // 默认值
        },
        method: "get",
      }, res => {
        console.log(res)

        // 自己收自己的
        if (res.code == 1) {
          wx.showToast({
            title: '接收成功',
            icon: 'none',
            duration: 2000
          })
          wx.switchTab ({
            url: '/pages/index/index',
          })
        } else {
          wx.showToast({
            title: res.data,
            icon: 'none',
            duration: 2000
          })
        }
       
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  onPageScroll: function (e) {
    if (e.scrollTop < 0) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})